package com.joseph.websocket;

import com.google.common.base.Stopwatch;
import com.joseph.websocket.client.NodeClient;
import com.joseph.websocket.util.JedisUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.Signal;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.time.StopWatch;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sound.midi.Soundbank;
import java.awt.*;
import java.net.InetSocketAddress;
import java.net.JarURLConnection;
import java.util.concurrent.TimeUnit;

@Log4j2
public class Server {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup wokerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap;
            serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, wokerGroup)
                    .channel(NioServerSocketChannel.class)
                    //.handler(new LoggingHandler(LogLevel.INFO))
                    //.option(ChannelOption.SO_KEEPALIVE, true)
                    .option(ChannelOption.ALLOCATOR, UnpooledByteBufAllocator.DEFAULT)
                    .childHandler(new WebSocketChannelInitializer());

            ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(8080)).sync();
//            StopWatch stopWatch = StopWatch.createStarted();
//            long start = System.currentTimeMillis();
//            JedisUtil.getJedis().get("joseph");
//            LOGGER.info("TIME1 {}",  System.currentTimeMillis() - start);
//            LOGGER.info("TIME {}", stopWatch.getTime());
//            StopWatch stopWatch1 = StopWatch.createStarted();
//            JedisUtil.getJedis().get("joseph");
//            LOGGER.info("TIME {}", stopWatch1.getTime());
//            //new NodeClient();
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    bossGroup.shutdownGracefully();
                    wokerGroup.shutdownGracefully();
                    log.info("ws server shutdown success!!!");
                }
            });
            log.info("server boot");
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            wokerGroup.shutdownGracefully();
        }
    }
}
